package main

import (
	"errors"
	"fmt"
)

type Printer func(contents string) (n int, err error)

func printTo(contents string) (n int, err error) {
	return fmt.Println(contents)
}

type operate func(x, y int) int

//高阶函数 可以满足下面2个条件:1.接收其他函数作为参数传入  2.把其他的函数作为结果返回
func calculate(x int, y int, op operate) (int, error) {
	if op == nil {
		return 0, errors.New("invalid operation")
	}
	return op(x, y), nil
}

type calculateFunc func(x int, y int) (int, error)

//高阶函数  接收一个函数  返回一个函数
func genCalculatorFunc(op operate) calculateFunc {
	return func(x int, y int) (int, error) {
		if op == nil {
			return 0, errors.New("invalid operation")
		}
		return op(x, y), nil
	}
}
func genCalculator(x int, y int, op operate) (int, error) {
	return func(x int, y int) (int, error) {
		if op == nil {
			return 0, errors.New("invalid operation")
		}
		return op(x, y), nil
	}(x, y)
}

func main() {

	var p Printer
	p = printTo
	p("huran")
	//op:= func(x,y int) int{
	//	return x+y
	//}(2,4)
	op := func(x, y int) int {
		return x + y
	}
	fmt.Println(op)
	fmt.Println("-----------------------------------")
	//传递一个函数

	i, _ := calculate(3, 5, op)
	fmt.Println(i)
	array1 := [3]string{"a", "b", "c"}
	fmt.Printf("The array:%v\n", array1)
	array2 := modifyArray(array1)
	fmt.Printf("The modified array:%v\n", array2)
	fmt.Printf("The original array:%v\n", array1)
}
func modifyArray(a [3]string) [3]string {
	a[1] = "x"
	return a
}
